package com.pennphoto.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.pennphoto.shared.FieldVerifier;

public class Login implements EntryPoint{

	private final LoginServiceAsync loginService = GWT.create(LoginService.class);
	private static final String SERVER_ERROR = "An error occurred while "
		+ "attempting to contact the server. Please check your network "
		+ "connection and try again.";
	@Override
	public void onModuleLoad() {
		// TODO Auto-generated method stub
		final Button loginButton = new Button("<font color=\"blue\"><i><b>Login</b></i></font>");
		final TextBox nameField = new TextBox();
		final PasswordTextBox nameField1 = new PasswordTextBox();
		final Button signupButton = new Button("<font color=\"blue\"><i><b>Signup</b></i></font>");
		final Label errorLabel = new Label();

		// We can add style names to widgets
		loginButton.addStyleName("loginButton");
		signupButton.addStyleName("signupButton");

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		final RootPanel rootPanel = RootPanel.get();
		rootPanel.add(nameField, 671, 206);
		nameField.setSize("150px", "18px");
		RootPanel.get().add(nameField1, 671, 250);
		nameField1.setSize("150px", "18px");
		RootPanel.get().add(loginButton, 710, 313);
		RootPanel.get().add(signupButton, 710, 415);
		signupButton.setSize("61px", "30px");
		RootPanel.get().add(errorLabel, 31, 27);
		errorLabel.setSize("860px", "20px");

		// Focus the cursor on the name field when the app loads
		nameField.setFocus(true);
		
		Label lblNotRegisteredYet = new Label((String) null);
		rootPanel.add(lblNotRegisteredYet, 433, 353);
		
		HTML htmlwelcomeToAlan = new HTML("<h1 align=\"center\"><b><u><font color=\"blue\">Welcome to <font color=\"orange\">PennPhoto</font></font></u></b></h1>", true);
		rootPanel.add(htmlwelcomeToAlan, 614, 27);
		htmlwelcomeToAlan.setSize("256px", "145px");
		
		HTML htmltheFieldsMarked = new HTML("<h3>The fields marked <font color=\"red\">*</font> are mandatory</h3>", true);
		rootPanel.add(htmltheFieldsMarked, 605, 265);
		
		HTML htmlusername = new HTML("<h3>Email:<font color=\"red\">*</font></h3>", true);
		rootPanel.add(htmlusername, 584, 197);
		
		HTML htmlpassword = new HTML("<h3>Password<font color=\"red\">*</font></h3>", true);
		rootPanel.add(htmlpassword, 584, 242);
		
		HTML htmlnotRegisteredYet = new HTML("<h3><font color=\"red\">Not Registered yet? Click Sign up</font></h3>", true);
		rootPanel.add(htmlnotRegisteredYet, 629, 359);
		
		HTML htmlNewHtml = new HTML("<img src=\"http://www.onlyinphiladelphia.com/wp-content/uploads/2010/06/upenn.jpg\" width=\"200\" height=\"200\"></img>", true);
		rootPanel.add(htmlNewHtml, 0, 10);
		htmlNewHtml.setSize("226px", "176px");
		
		HTML htmlNewHtml_1 = new HTML("<img src=\"http://www.innoworks.org/newinno/images/chapter_upenn.png\" width=\"200\" height=\"200\"></img>", true);
		rootPanel.add(htmlNewHtml_1, 218, 10);
		
		HTML htmlNewHtml_2 = new HTML("<img src=\"http://cs.nyu.edu/yinghu/pic/upenn1.jpg\" width=\"200\" height=\"200\"></img>", true);
		rootPanel.add(htmlNewHtml_2, 0, 242);
		htmlNewHtml_2.setSize("200px", "204px");
		
		HTML htmlNewHtml_3 = new HTML("<img src=\"http://namclo.linguistlist.org/images/UPenn1.jpg\" width=\"200\" height=\"200\"></img>", true);
		rootPanel.add(htmlNewHtml_3, 218, 242);
		nameField.selectAll();

		// Create the popup dialog box
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				loginButton.setEnabled(true);
				loginButton.setFocus(true);
			}
		});

		// Create a handler for the loginButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				sendNameToServer();
				//RootPanel.get().remove(0);
				//RootPanel.get().remove(0);
				//RootPanel.get().clear();
				//RootPanel.get().add(new Pan(nameField.getText()));
				
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					//RootPanel.get().clear();
					//RootPanel.get().add(new Pan(nameField.getText()));
					sendNameToServer();
				}
			}
		
			
			

			/**
			 * Send the name from the nameField to the server and wait for a response.
			 * @param  
			 */
			private void sendNameToServer() {
				// First, we validate the input.
				errorLabel.setText("");
				String textToServer = nameField.getText();
				String text1ToServer = nameField1.getText();
				if (FieldVerifier.isValidName(textToServer,text1ToServer)!=null) {
					String str=FieldVerifier.isValidName(textToServer,text1ToServer);
					//errorLabel.setText("Please enter at least four characters");
					errorLabel.setText(str);
					return;
				}

				// Then, we send the input to the server.
				loginButton.setEnabled(false);
				textToServerLabel.setText(textToServer);
				//textToServerLabel.setText(textToServer1);
				serverResponseLabel.setText("");
				loginService.getUserIdForLogin(textToServer,text1ToServer,
						new AsyncCallback<Integer>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox
										.setText("Remote Procedure Call - Failure");
								serverResponseLabel
										.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(SERVER_ERROR);
								dialogBox.center();
								closeButton.setFocus(true);
							}

							public void onSuccess(Integer result) {
								if (result!=-1){
									rootPanel.get().clear();
									UserProfile user =new UserProfile(result);
									user.onModuleLoad();
									
								}
								else {
								dialogBox.setText("Login Failed");
								serverResponseLabel
										.removeStyleName("Try Again");
								serverResponseLabel.setHTML("Login Failed");
								dialogBox.center();
								closeButton.setFocus(true);
								}
							}
						});
			}
		}
		
		// Create a handler for the signupButton
		class MyHandler1 implements ClickHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				//sendNameToServer();
				//RootPanel.get().remove(0);
				//RootPanel.get().remove(0);
				RootPanel.get().clear();
				new Register().onModuleLoad();
				//Register r =new Register();
				//r.onModuleLoad();
				//RootPanel.get().add(new Pan());
				//registration r  = new registration();
				//RootPanel.get().add(r);
				
			}
		

}
		
		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		loginButton.addClickHandler(handler);
		nameField.addKeyUpHandler(handler);
		nameField1.addKeyUpHandler(handler);
		
		// Add a handler to send the name to the server
				MyHandler1 handler1 = new MyHandler1();
				signupButton.addClickHandler(handler1);
		
	}
}
